Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / Streams-Patterns-Events / A Practical Guide / PG_06g_Data_Sharing.html
blob03d209834bfea7860112c0869bc50a8315504b42
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="1038.25">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ee}
22 span.s1 {color: #0000bf}
23 span.s2 {color: #007300}
24 span.s3 {color: #bf0000}
25 span.s4 {color: #000000}
26 span.s5 {color: #606060}
27 span.s6 {font: 12.0px Helvetica}
28 span.Apple-tab-span {white-space:pre}
29 table.t1 {border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080}
30 td.td1 {width: 27.0px; background-color: #cccccc; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
31 td.td2 {width: 126.0px; background-color: #cccccc; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
32 td.td3 {width: 276.0px; background-color: #cccccc; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
33 td.td4 {width: 27.0px; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
34 td.td5 {width: 126.0px; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
35 td.td6 {width: 276.0px; border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #808080 #808080 #808080 #808080; padding: 2.0px 5.0px 2.0px 5.0px}
36 </style>
37 </head>
38 <body>
39 <p class="p1"><b>Sharing data between patterns</b></p>
40 <p class="p2"><br></p>
41 <p class="p3">So far, we've seen patterns that are independent of each other. A single Pbind works on its own information, which is not available to other Pbinds. Also, for instance, the 'degree' pattern in a Pbind is not aware of what the 'dur' pattern is doing. Making these data available adds musical intelligence.</p>
42 <p class="p2"><br></p>
43 <p class="p3">There are a couple of distinct ways to transmit information from one pattern into another. The first, simpler, technique is to read values from the current event that is already being processed. The second is to pass information from one event pattern into a separate event pattern. Since both are event patterns, they produce different result events and the first technique does not apply.</p>
44 <p class="p2"><br></p>
45 <p class="p4"><b>Reading values from the current event</b></p>
46 <p class="p2"><br></p>
47 <p class="p3">Within a Pbind, value patterns can easily read from other values that have already been placed into the event. The <a href="../Patterns/Pkey.html"><span class="s1"><b>Pkey</b></span></a> pattern looks up the key in the event currently being processed and returns its value. From there, you can do any other pattern-friendly operation on it: filter patterns, math operators, etc.</p>
48 <p class="p2"><br></p>
49 <p class="p3"><b>Pkey(key):</b> Read the 'key' in the input event. Outputs values until the input event doesn't contain the key (i.e., the value is nil). There is no 'repeats' argument. If you need to limit the number of values, wrap Pkey in <a href="../Patterns/Pfin.html"><span class="s1">Pfin</span></a>.</p>
50 <p class="p2"><br></p>
51 <p class="p5">p = <span class="s1">Pkey</span>(<span class="s2">\a</span>).asStream;</p>
52 <p class="p6"><br></p>
53 <p class="p7">// The input value is an event with \a = 2, \b = 3; 'next' result is 2</p>
54 <p class="p5">p.next((a: 2, b: 3));</p>
55 <p class="p6"><br></p>
56 <p class="p7">// We can do math on the input event too</p>
57 <p class="p5">p = (<span class="s1">Pkey</span>(<span class="s2">\a</span>) * <span class="s1">Pkey</span>(<span class="s2">\b</span>)).asStream;</p>
58 <p class="p5">p.next((a: 2, b: 3));<span class="Apple-tab-span"> </span><span class="s3">// returns 6 == 2 * 3</span></p>
59 <p class="p2"><br></p>
60 <p class="p2"><br></p>
61 <p class="p3">In this simple example, staccato vs. legato is calculated based on scale degree: lower notes are longer and higher notes are shorter. That only scratches the surface of this technique!</p>
62 <p class="p2"><br></p>
63 <p class="p3">Be aware that Pkey can only look backward to keys stated earlier in the Pbind definition. Pbind processes the keys in the order given. In the example, it would not work to put 'legato' first and have it refer to 'degree' coming later, because the degree value is not available yet.</p>
64 <p class="p2"><br></p>
65 <p class="p7">// something simple - the higher the note, the shorter the length</p>
66 <p class="p5">(</p>
67 <p class="p5">p = <span class="s1">Pbind</span>(</p>
68 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pseq</span>([<span class="s1">Pseries</span>(-7, 1, 14), <span class="s1">Pseries</span>(7, -1, 14)], <span class="s1">inf</span>),</p>
69 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, 0.25,</p>
70 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// \degree is EARLIER in the Pbind</p>
71 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\legato</span>, <span class="s1">Pkey</span>(<span class="s2">\degree</span>).linexp(-7, 7, 2.0, 0.05)</p>
72 <p class="p5">).play;</p>
73 <p class="p5">)</p>
74 <p class="p6"><br></p>
75 <p class="p5">p.stop;</p>
76 <p class="p2"><br></p>
77 <p class="p2"><br></p>
78 <p class="p4"><b>Other information storage patterns</b></p>
79 <p class="p2"><br></p>
80 <p class="p3">These patterns represent three different strategies to persist information from one pattern and make it available to others.</p>
81 <p class="p2"><br></p>
82 <p class="p3"><b>Penvir(envir, pattern, independent):</b> The Streams that evaluate patterns are usually Routines, and Routines have the special feature of remembering the Environment that was in force the last time it yielded, and restoring the same environment the next time it's awakened. <a href="../Patterns/Penvir.html"><span class="s1">Penvir</span></a> establishes an environment in which 'pattern' will run. The environment can be initialized with values, or it could be empty at first and populated by elements of its pattern. The environment is separate from the event being processed (actually, the pattern could be either an event or value pattern). Access to the environment depends on function-driven patterns: Pfunc, Pfuncn, Proutine, .collect, .select, .reject, and similar.</p>
83 <p class="p2"><br></p>
84 <p class="p3">The 'independent' flag specifies whether the environment will be kept separate for each stream made from the Penvir. If true (the default), whenever the Penvir is embedded in a stream, a new environment is created that inherits the initial values provided by 'envir'. If false, the same environment is used for every stream. In that case, the same environment could also be used in different Penvir patterns, and modifications of the environment by one Penvir would carry over to all the others -- hence its usefulness for sharing data.</p>
85 <p class="p2"><br></p>
86 <p class="p3"><b>Pfset(func, pattern, cleanupFunc):</b> When embedded, <a href="../Patterns/Pfset.html"><span class="s1">Pfset</span></a> creates an environment and populates it using environment variable assignments in the provided function. For every 'next' call, the values in the preset environment are inserted into the event prototype before evaluating the child pattern. This is one way to set defaults for the pattern. It could also be used to load objects on the server, although this takes some care because the object would be reloaded every time the Pfset is played and you are responsible for freeing objects created this way in the cleanupFunc. (Pproto is another way; see <a href="PG_06f_Server_Control.html"><span class="s1">PG_06f_Server_Control</span></a>.)</p>
87 <p class="p2"><br></p>
88 <p class="p5">(</p>
89 <p class="p8">SynthDef<span class="s4">(</span><span class="s2">\playbuf</span><span class="s4">, { </span>|bufnum, start, dur = 1, amp = 0.2, out|</p>
90 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1">var</span><span class="Apple-tab-span"> </span>sig = <span class="s1">PlayBuf</span>.ar(1, bufnum, <span class="s1">BufRateScale</span>.ir(bufnum), 0, start);</p>
91 <p class="p5"><span class="Apple-tab-span"> </span>sig = sig * amp * <span class="s1">EnvGen</span>.kr(<span class="s1">Env</span>.linen(0.01, dur, 0.01), doneAction: 2);</p>
92 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar(out, sig ! 2)</p>
93 <p class="p5">}).add;</p>
94 <p class="p5">)</p>
95 <p class="p6"><br></p>
96 <p class="p5">(</p>
97 <p class="p5"><span class="s1">TempoClock</span>.default.tempo = 1;</p>
98 <p class="p5">p = <span class="s1">Pfset</span>({</p>
99 <p class="p9"><span class="s4"><span class="Apple-tab-span"> </span>~buf = </span><span class="s1">Buffer</span><span class="s4">.read(s, </span>"sounds/a11wlk01.wav"<span class="s4">);</span></p>
100 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>0.2.yield;<span class="Apple-tab-span"> </span></span>// sync seems to be incompatible with patterns</p>
101 <p class="p5"><span class="Apple-tab-span"> </span>~bufFrames = ~buf.numFrames;</p>
102 <p class="p8"><span class="s4">}, </span>Pbind<span class="s4">(</span></p>
103 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span></span>\instrument<span class="s4">, </span>\playbuf<span class="s4">,</span></p>
104 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// access resources in the protoevent by Pkey</p>
105 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span></span>\bufnum<span class="s4">, </span><span class="s1">Pkey</span><span class="s4">(</span>\buf<span class="s4">),</span></p>
106 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Pwhite</span>(1, 4, <span class="s1">inf</span>) * 0.25,</p>
107 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// upper bound of Pwhite is based on duration</p>
108 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// so that start + (dur * samplerate) never goes past the buffer's end</p>
109 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\start</span>, <span class="s1">Pwhite</span>(0, <span class="s1">Pkey</span>(<span class="s2">\bufFrames</span>) - (<span class="s1">Pkey</span>(<span class="s2">\dur</span>) * 44100), <span class="s1">inf</span>)</p>
110 <p class="p5">), { defer(inEnvir { <span class="s5">"freeing buffer"</span>.postln; ~buf.free }, 1.1) }).play;</p>
111 <p class="p5">)</p>
112 <p class="p6"><br></p>
113 <p class="p7">// shows a buffer number allocated ('true' ContiguousBlock)</p>
114 <p class="p5">s.bufferAllocator.debug;</p>
115 <p class="p6"><br></p>
116 <p class="p5">p.stop;</p>
117 <p class="p2"><br></p>
118 <p class="p2"><br></p>
119 <p class="p3"><b>Plambda(pattern, scope):</b> Maintains an 'eventScope' environment, that is attached to events while they're being processed. Values can be assigned into the event scope using Plet(key, pattern, return), and read from scope using Pget(key, default, repeats). Pget is somewhat similar to Pkey, but it has a 'repeats' argument controlling the number of return values as well as a 'default' that will be used if the given key is not found in the event scope.</p>
120 <p class="p2"><br></p>
121 <p class="p3">A unique feature of <a href="../Patterns/Plambda.html"><span class="s1">Plambda</span></a>/Plet/Pget is the ability for Plet to assign one value to the event scope and return another value to the main event simultaneously. Plet assigns the value from its 'pattern' into the event scope. The 'return' argument is optional; if provided, it gives the value to return back to Pbind.</p>
122 <p class="p2"><br></p>
123 <p class="p3">Plambda removes the eventScope before returning the final event to the caller. You can see the scope by tracing the inner pattern.</p>
124 <p class="p2"><br></p>
125 <p class="p8"><span class="s4">p = </span>Plambda<span class="s4">(</span></p>
126 <p class="p8"><span class="s4"><span class="Apple-tab-span"> </span></span>Pbind<span class="s4">(</span></p>
127 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\a</span>, <span class="s1">Plet</span>(<span class="s2">\z</span>, <span class="s1">Pseries</span>(0, 1, <span class="s1">inf</span>), <span class="s1">Pseries</span>(100, -1, <span class="s1">inf</span>)),</p>
128 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\b</span>, <span class="s1">Pget</span>(<span class="s2">\z</span>, 0, <span class="s1">inf</span>) * 2</p>
129 <p class="p5"><span class="Apple-tab-span"> </span>).trace(key: <span class="s2">\eventScope</span>, prefix: <span class="s5">"\nscope: "</span>)</p>
130 <p class="p5">).asStream;</p>
131 <p class="p6"><br></p>
132 <p class="p5">p.next(());</p>
133 <p class="p2"><br></p>
134 <p class="p3">Something similar can be done with Pkey, by using intermediate values in the event that don't correspond to any SynthDef control names. There's no harm in having extra values in the event that its synth will not use; only the required ones are sent to the server. Often this is simpler than Plambda, but there might be cases where Plambda is the only way.</p>
135 <p class="p2"><br></p>
136 <p class="p5">p = <span class="s1">Pbind</span>(</p>
137 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\z</span>, <span class="s1">Pseries</span>(0, 1, <span class="s1">inf</span>),</p>
138 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\a</span>, <span class="s1">Pseries</span>(100, -1, <span class="s1">inf</span>),</p>
139 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\b</span>, <span class="s1">Pkey</span>(<span class="s2">\z</span>) * 2</p>
140 <p class="p5">).asStream;</p>
141 <p class="p6"><br></p>
142 <p class="p5">p.nextN(5, ()).do(<span class="s1">_</span>.postln);</p>
143 <p class="p2"><br></p>
144 <p class="p2"><br></p>
145 <p class="p4"><b>Communicating values between separate event patterns</b></p>
146 <p class="p2"><br></p>
147 <p class="p3">Passing values from one Pbind to another takes a couple of little tricks. First is to store completed events in an accessible location. Neither the Pattern nor the EventStreamPlayer save the finished events; but, calling 'collect' on the pattern attaches a custom action to perform on every result event. Here, we save the event into an environment variable, but it could go into the global library, a declared variable or any other data structure.</p>
148 <p class="p2"><br></p>
149 <p class="p3">Second, we have to ensure that the source pattern is evaluated before any client patterns that depend on the source's value. The only way to do this is to schedule the source slightly earlier, because items scheduled at the same time on any clock can execute in any order. (There is no priority mechanism to make one thread always run first.) But, this scheduling requirement should not affect audio timing.</p>
150 <p class="p2"><br></p>
151 <p class="p3">The solution is a timing offset mechanism, which delays the sound of an event by a given number of beats. In the example, the bass pattern is scheduled 0.1 beats before whole-numbered beats (while the chord pattern runs exactly on whole-numbered beats). The bass pattern operates with a timing offset of 0.1, delaying the sound so that it occurs on integer beats. Both patterns sound together in the server, even though their timing is different in the client.</p>
152 <p class="p2"><br></p>
153 <table cellspacing="0" cellpadding="0" class="t1">
154 <tbody>
155 <tr>
156 <td valign="middle" class="td1">
157 <p class="p11"><b>Beat</b></p>
158 </td>
159 <td valign="middle" class="td2">
160 <p class="p11"><b>Client timing</b></p>
161 </td>
162 <td valign="middle" class="td3">
163 <p class="p11"><b>Server timing</b></p>
164 </td>
165 </tr>
166 <tr>
167 <td valign="middle" class="td4">
168 <p class="p11"><b>0.9</b></p>
169 </td>
170 <td valign="middle" class="td5">
171 <p class="p3">Bass event calculated</p>
172 </td>
173 <td valign="middle" class="td6">
174 <p class="p3">(bass event delayed by 0.1, nothing happens here)</p>
175 </td>
176 </tr>
177 <tr>
178 <td valign="middle" class="td4">
179 <p class="p11"><b>1.0</b></p>
180 </td>
181 <td valign="middle" class="td5">
182 <p class="p3">Chord event calculated</p>
183 </td>
184 <td valign="middle" class="td6">
185 <p class="p3">Both bass and chord make sound</p>
186 </td>
187 </tr>
188 </tbody>
189 </table>
190 <p class="p2"><br></p>
191 <p class="p5">(</p>
192 <p class="p5"><span class="s1">TempoClock</span>.default.tempo = 1;</p>
193 <p class="p6"><br></p>
194 <p class="p5">~bass = <span class="s1">Pbind</span>(</p>
195 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pwhite</span>(0, 7, <span class="s1">inf</span>),</p>
196 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">\octave</span><span class="s4">, 3,<span class="Apple-tab-span"> </span></span>// down 2 octaves</p>
197 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Pwhite</span>(1, 4, <span class="s1">inf</span>),</p>
198 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span></span>\legato<span class="s4">, 1,</span></p>
199 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.2</p>
200 <p class="p5">).collect({ <span class="s1">|event|</span></p>
201 <p class="p5"><span class="Apple-tab-span"> </span>~lastBassEvent = event;</p>
202 <p class="p5">}).play(quant: <span class="s1">Quant</span>(quant: 1, timingOffset: 0.1));</p>
203 <p class="p6"><br></p>
204 <p class="p7">// shorter form for the Quant object: #[1, 0, 0.1]</p>
205 <p class="p6"><br></p>
206 <p class="p5">~chords = <span class="s1">Pbind</span>(</p>
207 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\topNote</span>, <span class="s1">Pseries</span>(7, <span class="s1">Prand</span>(#[-2, -1, 1, 2], <span class="s1">inf</span>), <span class="s1">inf</span>).fold(2, 14),</p>
208 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\bassTriadNotes</span>, <span class="s1">Pfunc</span> { ~lastBassEvent[<span class="s2">\degree</span>] } + #[0, 2, 4],</p>
209 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// merge triad into topnote</p>
210 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// raises triad notes to the highest octave lower than top note</p>
211 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// div: is integer division, so x div: 7 * 7 means the next lower multiple of 7</p>
212 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span></span>\merge<span class="s4">, (</span><span class="s1">Pkey</span><span class="s4">(</span>\topNote<span class="s4">) - </span><span class="s1">Pkey</span><span class="s4">(</span>\bassTriadNotes<span class="s4">)) div: 7 * 7 + </span><span class="s1">Pkey</span><span class="s4">(</span>\bassTriadNotes<span class="s4">),</span></p>
213 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// add topNote to the array if not already there</p>
214 <p class="p8"><span class="s4"><span class="Apple-tab-span"> </span></span><span class="s2">\degree</span><span class="s4">, </span>Pfunc<span class="s4"> { </span>|ev|</p>
215 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>if(ev[<span class="s2">\merge</span>].detect({ <span class="s1">|item|</span> item == ev[<span class="s2">\topNote</span>] }).isNil) {</p>
216 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>ev[<span class="s2">\merge</span>] ++ ev[<span class="s2">\topNote</span>]</p>
217 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>} {</p>
218 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>ev[<span class="s2">\merge</span>]</p>
219 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}</p>
220 <p class="p5"><span class="Apple-tab-span"> </span>},</p>
221 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Pwrand</span>([<span class="s1">Pseq</span>([0.5, <span class="s1">Pwhite</span>(1, 3, 1), 0.5], 1), 1, 2, 3], #[1, 3, 2, 2].normalizeSum, <span class="s1">inf</span>),</p>
222 <p class="p5"><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.05</p>
223 <p class="p5">).play(quant: 1);</p>
224 <p class="p5">)</p>
225 <p class="p6"><br></p>
226 <p class="p5">~bass.stop;</p>
227 <p class="p5">~chords.stop;</p>
228 <p class="p2"><br></p>
229 <p class="p3">The chord pattern demonstrates some of the ways higher-level logic can be expressed in patterns. The goal is to transpose the notes of the root position triad over the bass note by octave so that the notes all fall within the octave beneath a top note (chosen by stepwise motion). Pkey(\topNote) - Pkey(\bassTriadNotes) gives the number of transposition steps to bring the triad notes up to the top note; then the transposition steps are truncated to the next lower octave (x div: 7 is integer division producing an octave number; multiplying by 7 gives the number of scale degrees for that octave).</p>
230 <p class="p2"><br></p>
231 <p class="p8"><span class="s4">f = { </span>|topNote, triad|</p>
232 <p class="p5"><span class="Apple-tab-span"> </span><span class="s1">var</span><span class="Apple-tab-span"> </span>x;</p>
233 <p class="p5"><span class="Apple-tab-span"> </span>x = (topNote - triad).debug(<span class="s5">"initial transposition steps"</span>);</p>
234 <p class="p5"><span class="Apple-tab-span"> </span>x = (x div: 7).debug(<span class="s5">"octaves to transpose"</span>);</p>
235 <p class="p5"><span class="Apple-tab-span"> </span>x = (x * 7).debug(<span class="s5">"steps to transpose"</span>);</p>
236 <p class="p5"><span class="Apple-tab-span"> </span>x + triad</p>
237 <p class="p5">};</p>
238 <p class="p6"><br></p>
239 <p class="p5">f.value(7, #[0, 2, 4]);</p>
240 <p class="p3">--&gt; [ 7, 2, 4 ] (first inversion triad)</p>
241 <p class="p2"><br></p>
242 <p class="p3">Then the transposed array is checked to see if the top note is already a member. If not, it's added so that the melody will always be present.</p>
243 <p class="p2"><br></p>
244 <p class="p3">Note that lazy operations on patterns define most of this behavior; only the conditional array check had to be written as a function.</p>
245 <p class="p2"><br></p>
246 <p class="p2"><br></p>
247 <p class="p3">The above example breaks one of the design principles of patterns. Ideally, it should be possible to play a single pattern object many times simultaneously without the different streams interfering with each other. Saving the bass note in one environment variable means that concurrent streams would not work together because they can't both use the same environment variable at the same time. The above approach does, however, allow the two patterns to be stopped and started independently, and new bass-dependent patterns to be added at any time. In some musical scenarios, this kind of flexibility is more important than respecting the pattern design ideal.</p>
248 <p class="p2"><br></p>
249 <p class="p3">It is possible, using <a href="../Patterns/Ptpar.html"><span class="s1">Ptpar</span></a> and <a href="../Patterns/Penvir.html"><span class="s1">Penvir</span></a>, to create independent environments for event storage as part of the pattern itself. By default, Penvir creates a new copy of its environment for each stream, guaranteeing independence. While the pattern is running, ~lastBassEvent = event saves the event in the stream's copy of the storage environment, and it's available to both Pbinds because both are under control of Penvir (indirectly through Ptpar).</p>
250 <p class="p2"><br></p>
251 <p class="p5">(</p>
252 <p class="p5">p = <span class="s1">Penvir</span>((), <span class="s1">Ptpar</span>([</p>
253 <p class="p5"><span class="Apple-tab-span"> </span>0.0, <span class="s1">Pbind</span>(</p>
254 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\degree</span>, <span class="s1">Pwhite</span>(0, 7, <span class="s1">inf</span>),</p>
255 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">\octave</span><span class="s4">, 3,<span class="Apple-tab-span"> </span></span>// down 2 octaves</p>
256 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Pwhite</span>(1, 4, <span class="s1">inf</span>),</p>
257 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\legato<span class="s4">, 1,</span></p>
258 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.2,</p>
259 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\timingOffset<span class="s4">, 0.1</span></p>
260 <p class="p5"><span class="Apple-tab-span"> </span>).collect({ <span class="s1">|event|</span></p>
261 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>~lastBassEvent = event;</p>
262 <p class="p5"><span class="Apple-tab-span"> </span>}),</p>
263 <p class="p5"><span class="Apple-tab-span"> </span>0.1, <span class="s1">Pbind</span>(</p>
264 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\topNote</span>, <span class="s1">Pseries</span>(7, <span class="s1">Prand</span>(#[-2, -1, 1, 2], <span class="s1">inf</span>), <span class="s1">inf</span>).fold(2, 14),</p>
265 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\bassTriadNotes</span>, <span class="s1">Pfunc</span> { ~lastBassEvent[<span class="s2">\degree</span>] } + #[0, 2, 4],</p>
266 <p class="p10"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>\merge<span class="s4">, (</span><span class="s1">Pkey</span><span class="s4">(</span>\topNote<span class="s4">) - </span><span class="s1">Pkey</span><span class="s4">(</span>\bassTriadNotes<span class="s4">)) div: 7 * 7 + </span><span class="s1">Pkey</span><span class="s4">(</span>\bassTriadNotes<span class="s4">),</span></p>
267 <p class="p8"><span class="s4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s2">\degree</span><span class="s4">, </span>Pfunc<span class="s4"> { </span>|ev|</p>
268 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>if(ev[<span class="s2">\merge</span>].detect({ <span class="s1">|item|</span> item == ev[<span class="s2">\topNote</span>] }).isNil) {</p>
269 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>ev[<span class="s2">\merge</span>] ++ ev[<span class="s2">\topNote</span>]</p>
270 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>} {</p>
271 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>ev[<span class="s2">\merge</span>]</p>
272 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}</p>
273 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>},</p>
274 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\dur</span>, <span class="s1">Pwrand</span>([<span class="s1">Pseq</span>([0.5, <span class="s1">Pwhite</span>(1, 3, 1), 0.5], 1), 1, 2, 3], #[1, 3, 2, 2].normalizeSum, <span class="s1">inf</span>),</p>
275 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">\amp</span>, 0.05</p>
276 <p class="p5"><span class="Apple-tab-span"> </span>)</p>
277 <p class="p5">])).play;</p>
278 <p class="p5">)</p>
279 <p class="p6"><br></p>
280 <p class="p5">p.stop;</p>
281 <p class="p2"><br></p>
282 <p class="p2"><br></p>
283 <p class="p12"><span class="s4">Previous:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_06f_Server_Control.html"><span class="s6">PG_06f_Server_Control</span></a></span></p>
284 <p class="p12"><span class="s4">Next:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_07_Value_Conversions.html"><span class="s6">PG_07_Value_Conversions</span></a></span></p>
285 </body>
286 </html>